import sys
import time
from constants import APP_PACKAGE, DEVICES, PHONES, COORDINATE
from login import login

import uiautomator2 as u2
from uiautomator2 import Direction

isLogin = False


def empty():
    print("初始化条件")


# def login(d):
#     # 判断登录页面是否存在
#     if not d(text="登录/注册").exists:
#         print("账号已经登录了，请退出重试！！")
#         sys.exit()
#
#     print('正常登录')
#
#     # 勾选 弹窗同意
#     if d(text='同意').exists:
#         d(text='同意').click()
#
#     # 输入手机
#     d(text='请输入手机号').set_text(18193995509)
#     # 输入验证码
#     d(text='请输入验证码').set_text(666888)
#     # 点击同意协议
#     d.click(0.169, 0.458)
#
#     # 点击登录
#     d(resourceId='login_btn').click()
#     return True


def third_login():
    print("第三方登录")


def monitor_click():
    print('模拟点击')


def go_cmd(cmd):

    return 'cmd'


def logout(d):
    # 点击tab个人
    d.click(0.872, 0.966)
    d.click(timeout=1.0)
    # 退出登录
    d.click(0.422, 0.793)
    d.click(0.804, 0.584)
    return False


def tab_test(d):
    d(text="流月运势").click()
    time.sleep(1.0)
    d(text="流年运势").click()
    time.sleep(1.5)
    d(resourceId="cai_tab_day").click()


def kai_yun_time(d):
    ids = [f'yun_{x}' for x in range(1, 14)]
    for x in ids:
        d(resourceId=x).click()
        time.sleep(0.1)


# 测试流月每个月份
def tab_per_month(d):
    ids = [f'tap_month_list_{x}' for x in range(1, 13)]
    for x in ids:
        d(resourceId=x).click()
        time.sleep(0.2)
        # swipe
        d.swipe_ext(Direction.UP, scale=0.3)


# 去看八字报告
def home_bz_report(d):
    d(text="八字报告").click()


def change_material(d):
    d(resourceId='link_user_avatar').click()


def tab_grid(d):
    # 铁板神术 TODO
    for x in COORDINATE['grid']:
        d.click(x[0], x[1])
        time.sleep(1.5)
        d.press('back')


def un_or_install(d):
    # 卸载APP
    code = d.app_uninstall(APP_PACKAGE)

    if code:
        print(APP_PACKAGE, "卸载成功!")
    else:
        print(APP_PACKAGE, "卸载失败或未安装!")

    # 安装APP
    print(APP_PACKAGE, "下载中...")
    d.app_install('https://www.tiansuixiansheng.com/suan2/12-24.apk')


def print_hi(name):
    print(f'Hi, {name} Ready for play?')  # Press Ctrl+F8 to toggle the breakpoint.
    # adb devices
    d = u2.connect(DEVICES['vivo'])
    # 设置最大查找元素时间
    d.implicitly_wait(1.0)
    # 卸载 APP 并 安装新的
    # un_or_install(d)
    d.app_start(APP_PACKAGE)
    change_material(d)
    # tab_per_month(d)
    # tab_grid(d)
    # kai_yun_time(d)
    # tab_test(d)
    # login(d, PHONES['com1'])
    # logout(d)


# d.app_uninstall(APP_PACKAGE)

# d.app_start('com.sina.weibo')
# d.app_stop_all() # 停止所有应用
# sina = d.app_info('com.sina.weibo')
# d.push('./meriodas.webp', "/sdcard/")
# d.pull("/sdcard/DCIM/Screenshots/123.jpg", "123.jpg")
# d.app_wait("com.a169kai.tiansui")  # 开启app
# print(d.info)  # 打印设备信息
# login_btn = d(resourceId='login_btn')
# login_btn.click()  # 点击事件


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('小佩佩')
    # 1打开文件
    # 2扫描 testID属性
    # 3生成ID树

    # 4编写规则

    # 5执行规则

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
